package cn.iocoder.yudao.module.jky.controller.admin.goodsattributevalue;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jky.controller.admin.goodsattributevalue.vo.*;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattributevalue.GoodsAttributeValueDO;
import cn.iocoder.yudao.module.jky.convert.goodsattributevalue.GoodsAttributeValueConvert;
import cn.iocoder.yudao.module.jky.service.goodsattributevalue.GoodsAttributeValueService;

@Tag(name = "管理后台 - 商品属性值")
@RestController
@RequestMapping("/supplier/goods-attribute-value")
@Validated
public class GoodsAttributeValueController {

    @Resource
    private GoodsAttributeValueService goodsAttributeValueService;

    @PostMapping("/create")
    @Operation(summary = "创建商品属性值")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:create')")
    public CommonResult<Integer> createGoodsAttributeValue(@Valid @RequestBody GoodsAttributeValueCreateReqVO createReqVO) {
        return success(goodsAttributeValueService.createGoodsAttributeValue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品属性值")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:update')")
    public CommonResult<Boolean> updateGoodsAttributeValue(@Valid @RequestBody GoodsAttributeValueUpdateReqVO updateReqVO) {
        goodsAttributeValueService.updateGoodsAttributeValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品属性值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:delete')")
    public CommonResult<Boolean> deleteGoodsAttributeValue(@RequestParam("id") Integer id) {
        goodsAttributeValueService.deleteGoodsAttributeValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品属性值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:query')")
    public CommonResult<GoodsAttributeValueRespVO> getGoodsAttributeValue(@RequestParam("id") Integer id) {
        GoodsAttributeValueDO goodsAttributeValue = goodsAttributeValueService.getGoodsAttributeValue(id);
        return success(GoodsAttributeValueConvert.INSTANCE.convert(goodsAttributeValue));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品属性值列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:query')")
    public CommonResult<List<GoodsAttributeValueRespVO>> getGoodsAttributeValueList(@RequestParam("ids") Collection<Integer> ids) {
        List<GoodsAttributeValueDO> list = goodsAttributeValueService.getGoodsAttributeValueList(ids);
        return success(GoodsAttributeValueConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品属性值分页")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:query')")
    public CommonResult<PageResult<GoodsAttributeValueRespVO>> getGoodsAttributeValuePage(@Valid GoodsAttributeValuePageReqVO pageVO) {
        PageResult<GoodsAttributeValueDO> pageResult = goodsAttributeValueService.getGoodsAttributeValuePage(pageVO);
        return success(GoodsAttributeValueConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品属性值 Excel")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute-value:export')")
    @OperateLog(type = EXPORT)
    public void exportGoodsAttributeValueExcel(@Valid GoodsAttributeValueExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GoodsAttributeValueDO> list = goodsAttributeValueService.getGoodsAttributeValueList(exportReqVO);
        // 导出 Excel
        List<GoodsAttributeValueExcelVO> datas = GoodsAttributeValueConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品属性值.xls", "数据", GoodsAttributeValueExcelVO.class, datas);
    }

}
